package com.example.takeaway.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.example.takeaway.common.R;
import com.example.takeaway.entity.Employee;
import com.example.takeaway.service.EmployeeService;
import com.fasterxml.jackson.databind.JsonSerializer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.service.Tags;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Slf4j
@Api(tags = "用户")
@RestController
@RequestMapping("/employee")
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeService employeeService;


    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request , @RequestBody Employee employee){



        //将密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //根据账号进行数据库查询
        //条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>()
                .eq(Employee::getUsername, employee.getUsername());

        //查询
        Employee emp = employeeService.getOne(queryWrapper);

        //如果没有查到 返回登录失败信息
        if(BeanUtil.isEmpty(emp)){
            return R.error("登录失败 账号不存在");
        }

        //比对密码是否一致
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败 密码或账号错误");
        }

        //查看员工是否被禁用 ---》 是 ——————》返会登录失败
        if(emp.getStatus() == 0){
            return R.error("账号被禁用");
        }

        //成功
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);


    }

    /**
     * 员工退出
     * @param request
     * @return
     */

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){

        //清除session中保存当前员工登录的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    @ApiModelProperty("添加员工")
    @PostMapping
    public R<String> save(HttpServletRequest httpServletRequest , @RequestBody  Employee employee) {


        log.info("新增员工 员工信息：{}",employee.toString());


        //设置初始密码 123456 md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));


        //获取当前登录id
        Long empId = (Long) httpServletRequest.getSession().getAttribute("employee");

        employeeService.save(employee);



        return R.success("新增员工成功");



    }


    /**
     *员工分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @ApiModelProperty("查询员工")
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page={},pageSize={},name = {}",page,pageSize,name);


        //构建分页构造器
        Page page1 = new Page(page, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<Employee>()
                .like(StringUtils.isNotEmpty(name), Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime);


        //执行查询条件
        employeeService.page(page1,employeeLambdaQueryWrapper);  //结果会自动封装到Page里面

        return R.success(page1);
    }


    /**
     * 更新员工信息
     * @param employee
     * @return
     */

    @ApiModelProperty("更新员工信息")
    @PutMapping
    public R<String> update(HttpServletRequest request , @RequestBody  Employee employee){
        log.info(employee.toString());

        long id = Thread.currentThread().getId();
        log.info("当先线程的id是：{}",id);


        Long empId = (Long)request.getSession().getAttribute("employee");


        employeeService.updateById(employee);


        return R.success("员工信息修改成功");
    }


    @ApiModelProperty("根据id查询用户")
    @GetMapping("/{id}")
    public R<Employee> SelectById(@PathVariable Long id){
        log.info("界面回显");
        Employee employee = employeeService.getById(id);

        return R.success(employee);
    }



}
